package com.skyline.jfs.cluster.service.impl;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.jfs.cluster.dao.JFSNodeDao;
import com.skyline.jfs.cluster.model.JFSNode;
import com.skyline.jfs.cluster.model.JFSNodeConfigProper;
import com.skyline.jfs.cluster.service.LocalFSInfoSerivce;
import com.skyline.jfs.common.config.Config;
import com.skyline.jfs.namespace.dao.JFSDirectoryDao;
import com.skyline.jfs.namespace.dao.JFSFileDao;
import com.skyline.jfs.storage.dao.JFSStorageDao;
import com.skyline.jfs.storage.server.JFSNodeServer;

@Service("localFSInfoSerivce")
public class LocalFSInfoSerivceImpl implements LocalFSInfoSerivce {

	@Autowired
	private JFSNodeDao jfsNodeDao;
	
	@Autowired
	private JFSFileDao jfsFileDao;

	@Autowired
	private JFSNodeServer server;
	
	@Autowired
	private JFSDirectoryDao jfsDirectoryDao;
	
	@Autowired 
	private JFSStorageDao jfsStorageDao;

	private static boolean isFirtTimeInital = true;

	private void getFSInfo() {
		String storageDirectory = JFSNodeConfigProper.getStorageDirectory();
		if (storageDirectory != null) {
			File storageRoot = new File(storageDirectory);
			if (storageRoot.exists()) {
				JFSNodeConfigProper.setTotalSpace(storageRoot.getTotalSpace());
				JFSNodeConfigProper.setUsableSpace(storageRoot.getUsableSpace());
			} else {
				JFSNodeConfigProper.setTotalSpace(Long.valueOf(0));
				JFSNodeConfigProper.setUsableSpace(Long.valueOf(0));
			}
		} else {
			JFSNodeConfigProper.setTotalSpace(Long.valueOf(0));
			JFSNodeConfigProper.setUsableSpace(Long.valueOf(0));
		}
	}

	public void initalLocalFSInfoForce() {
		JFSNode locaNode = jfsNodeDao.queryJFSNodeByID(JFSNodeConfigProper.getId());
		if (locaNode == null) {
			jfsNodeDao.insertJFSNode();
		} else {
			JFSNodeConfigProper.setAdminJobNum(locaNode.getAdminiJobNum());
			JFSNodeConfigProper.setDescription(locaNode.getDescription());
			JFSNodeConfigProper.setHostname(locaNode.getName());
			JFSNodeConfigProper.setIpAddress(locaNode.getIp());
			JFSNodeConfigProper.setLoadbalacingFactor(locaNode.getLoadbalancingFactor());
			JFSNodeConfigProper.setRegisted(true);
			JFSNodeConfigProper.setStorageDirectory(locaNode.getStorageDirctory());
		}
	}

	private void getInitalProperFromProper() {
		JFSNodeConfigProper.setId(Integer.valueOf(Config.getLocalFSConfig("hostid")));
		JFSNodeConfigProper.setAdminJobNum(Long.valueOf(Config.getLocalFSConfig("administrator_jobNum")));
		JFSNodeConfigProper.setDescription("this is JFS_NODE1");
		JFSNodeConfigProper.setHostname(Config.getLocalFSConfig("hostname"));
		JFSNodeConfigProper.setIpAddress(Config.getLocalFSConfig("IP_Address"));
		JFSNodeConfigProper.setLoadbalacingFactor(Integer.valueOf(Config.getLocalFSConfig("loadbalacing_factor")));
		JFSNodeConfigProper.setRegisted(true);
		JFSNodeConfigProper.setStorageDirectory(Config.getLocalFSConfig("storageDirectory"));
	}

	public void initalLocalFSInfo() {
		if (isFirtTimeInital == true) {
			getInitalProperFromProper();
			getFSInfo();
			initalLocalFSInfoForce();
			isFirtTimeInital = false;
			server.initalServer(jfsFileDao,jfsDirectoryDao,jfsStorageDao);
		}
		getFSInfo();
	}
}
